package com.esiran.payadmin.shiro;

import com.esiran.payadmin.shiro.filter.AdminAuthFilter;
import com.esiran.payadmin.shiro.filter.UserAuthFilter;
import com.esiran.payadmin.shiro.realm.SystemUserRealm;
import com.esiran.payadmin.shiro.realm.UserRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean("sessionManager")
    public SessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(false);
        sessionManager.setSessionIdCookieEnabled(false);
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
    @Bean("securityManager")
    public SecurityManager securityManager(SessionManager sessionManager,
                                           UserRealm userRealm, SystemUserRealm systemUserRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealms(Arrays.asList(new Realm[]{userRealm, systemUserRealm}));
        securityManager.setSessionManager(sessionManager);
//        securityManager.setSubjectFactory();
        return securityManager;
    }
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        Map<String, Filter> filterMap = new LinkedHashMap<>();

        // 前台用户拦截器
        UserAuthFilter userAuthFilter = new UserAuthFilter();
        userAuthFilter.setLoginUrl("/user/login");
        userAuthFilter.setSuccessUrl("/user");
        // 后台用户拦截器
        AdminAuthFilter adminAuthFilter = new AdminAuthFilter();
        adminAuthFilter.setLoginUrl("/admin/login");
        adminAuthFilter.setSuccessUrl("/admin");

        filterMap.put("user_filter",userAuthFilter);
        filterMap.put("admin_filter",adminAuthFilter);

        shiroFilter.setFilters(filterMap);
        Map<String,String> filterChain = new LinkedHashMap<>();
        filterChain.put("/user/**","user_filter");
        filterChain.put("/admin/**","admin_filter");
        shiroFilter.setFilterChainDefinitionMap(filterChain);
        return shiroFilter;
    }
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
